﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WinChatListen.Model;

namespace WinChatListen.Utils
{
    public class Api
    {

        private static SubmitHtml submitHtml = new SubmitHtml();
        private static String url = "http://127.0.0.1:19088/api/";

        public static bool IsLogin() {
            String result = submitHtml.Submit(url+ "?type=0", "", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public static UserInfo getUserInfo()
        {
            String result = submitHtml.Submit(url + "?type=1", "", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if ((int)jObject["code"] == 1) {
                String dataStr = jObject["data"].ToString();
                UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(dataStr);
                return userInfo;
            }
            return null;
        }
        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="wxid"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool sendMessageText(String wxid, String msg)
        {
            String result = submitHtml.Submit(url + "?type=2", "{\"wxid\":\""+ wxid + "\",\"msg\":\""+msg+ "\"}", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }
        /// <summary>
        /// 发送图片消息
        /// </summary>
        /// <param name="wxid"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool sendMessageImage(String wxid, String imagePath)
        {
            String result = submitHtml.Submit(url + "?type=5", "{\"wxid\":\"" + wxid + "\",\"imagePath\":\"" + imagePath + "\"}", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }
        /// <summary>
        /// 发送文件消息
        /// </summary>
        /// <param name="wxid"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool sendMessageFile(String wxid, String filePath)
        {
            String result = submitHtml.Submit(url + "?type=6", "{\"wxid\":\"" + wxid + "\",\"filePath\":\"" + filePath + "\"}", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }
        /// <summary>
        /// 获取联系人包括群
        /// </summary>
        public static void getFriendsRooms(ref List<Friend> friends, ref List<Friend> rooms)
        {
            String result = submitHtml.Submit(url + "?type=46", "", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if ((int)jObject["code"] == 1)
            {
                String dataStr = jObject["data"].ToString();
                List<Friend> friendTemps = JsonConvert.DeserializeObject<List<Friend>>(dataStr);
                friends = new List<Friend>();
                rooms = new List<Friend>();
                foreach (Friend friend in friendTemps)
                {
                    if (friend.type == 1) {
                        friends.Add(friend);
                    }
                    if (friend.type == 2) {
                        rooms.Add(friend);
                    }
                }
            }
        }
        /// <summary>
        /// hook消息
        /// </summary>
        /// <returns></returns>
        public static bool hookMessage()
        {
            String result = submitHtml.Submit(url + "?type=9", "{\"ip\":\"127.0.0.1\",\"port\":19099}", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }
        /// <summary>
        /// 取消hook消息
        /// </summary>
        /// <returns></returns>
        public static bool unhookMessage()
        {
            String result = submitHtml.Submit(url + "?type=10", "", SubmitHtml.SubmitMethod.POST);
            JObject jObject = JsonConvert.DeserializeObject<JObject>(result);
            if (jObject["result"].ToString() == "OK")
                return true;
            return false;
        }
    }
}
